Utilisateur:Esby/Multilingual description.js

Une page de Wikipédia, l'encyclopédie libre.
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
/**
 * source: http://commons.wikimedia.org/w/index.php?title=MediaWiki:Multilingual_description.js&oldid=46082731
 * 
 * to benefit of [[:Template:Multilingual description]]
 * 
 * Implements language selection for multilingual elements
 * 
 * In certain environments, it's not feasible to neatly box away each
 * different language into its own section of the site. By marking elements
 * multilingual, you can emulate this behavior by only displaying the 
 * message in the user's language. This reduces the "Tower of Babel" effect.
 * 
 * @author Edward Z. Yang (Ambush Commander), Rewritten by DieBuche
 */

if (typeof $ == 'undefined') $=jQuery;


/* Configuration: */

// in your monobook.js, set ls_enable = false to stop the javascript
// the cookie name we use to stash the info.
var multilingual = {

	cookie: 'commonswiki_language_js',

	// link to the language select page
	helpUrl: 'http://meta.wikimedia.org/wiki/Meta:Language_select',

	// strings that are part of the widgets
	stringHelp: {
		'cs': 'Výběr jazyka:',
		'de': 'Sprachauswahl:',
		'en': 'Language select:',
		'eo': 'Lingvoelekto:',
		'fr': 'Selecteur de langue:',
		'ko': '언어 선택:',
		'mk': 'Јазик:',
		'ml': 'ഭാഷ തിരഞ്ഞെടുക്കുക:',
		'nds': 'Spraakutwahl:',
		'nl': 'Taal:',
		'pl': 'Wybierz język:',
		'ru': 'Выбор языка:'
	},
	stringSelect: {
		'cs': 'Vybrat',
		'de': 'Auswahl',
		'en': 'Select',
		'eo': 'Elekti',
		'fr': 'Seulement ce langage',
		'ko': '선택',
		'mk': 'Одбери',
		'ml': 'തിരഞ്ഞെടുക്കുക',
		'nds': 'Utwählen',
		'nl': 'Selecteer',
		'pl': 'Wybierz',
		'ru': 'Выбрать'
	},
	stringShowAll: {
		'cs': 'Zobrazit všechny',
		'de': 'Alle anzeigen',
		'en': 'Show all',
		'eo': 'ĉiuj',
		'fr': 'Tous les langages',
		'ko': '모두 보기',
		'mk': 'Сите',
		'ml': 'എല്ലാം',
		'nds': 'All wiesen',
		'nl': 'Toon alles',
		'pl': 'Pokaż wszystkie',
		'ru': 'Показать все'
	},


	// autodetects a browser language
	getBrowserLanguage: function () {
		return navigator.userLanguage || navigator.language || navigator.browserLanguage;
	},

	// sets a new language to the cookie
	setCookieLanguage: function (language) {
                if (wgNoticeProject != 'commons') return "";
		$.cookie(this.cookie, escape(language), {
			expires: 100,
			path: '/'
		});
	},
	// deletes the cookie
	deleteCookieLanguage: function (language) {
                if (wgNoticeProject != 'commons') return "";
		$.cookie(this.cookie, null, {
			path: '/'
		});
	},
	deleteOldCookieLanguage: function (language) {
                if (wgNoticeProject != 'commons') return "";
		$.cookie(this.cookie, null, {
			path: '/wiki'
		});
	},
	// grabs the ISO 639 language code based
	// on either the browser or a supplied cookie
	getLanguage: function () {
		var language = '';

		// Priority:
		//  1. Cookie (only for commons)
		//  2. mw.config.get('wgUserLanguage') global variable
		//  3. Browser autodetection
		// grab according to cookie
                if (wgNoticeProject == 'commons') language = $.cookie(this.cookie);

		// grab according to mw.config.get('wgUserLanguage') if user is logged in
		if (!language && window.wgUserLanguage && mw.config.get('wgUserGroups') !== null) {
			language = mw.config.get('wgUserLanguage');
		}

		// grab according to browser if none defined
		if (!language) language = this.getBrowserLanguage();

		// inflexible: can't accept multiple languages
		// remove dialect/region code, leaving only the ISO 639 code
		language = language.replace(/-.*?/, '');

		return language;
	},

	// build widget for changing the language cookie
	buildWidget: function (language) {

		this.$container = $('<div></div>');
		// link to language select description page
		this.$container.html('<a href="' + this.helpUrl + '" class="ls_link">' + this.stringHelpText + '</a> ');
		this.$select = $('<select></select>');


		var seen = {};
		$('[lang]').each(function () {
			var lang = $(this).attr('lang');
			if (!seen[lang]) {
				seen[lang] = true;
				multilingual.$select.append('<option>' + lang + '</option>');
			}
		});
		this.$select.prepend('<option value="showall">' + this.stringShowallText + '</option>');
		this.$select.attr('id', 'langselector');
		this.$select.val(this.getLanguage());
		this.$select.change(function () {
			multilingual.setCookieLanguage($('#langselector').val());
			multilingual.apply($('#langselector').val());
		});
		this.$container.append(this.$select);
		if ($('#file').length) $('#file').append(this.$container);
		else $('#bodyContent').prepend(this.$container);

	},

	// main body of the function
	init: function () {
		//if (typeof(ls_enable) != 'undefined') return;
		
		
		this.stringHelpText = (this.stringHelp[mw.config.get('wgUserLanguage')] || this.stringHelp['en']);
		this.stringShowallText = (this.stringShowAll[mw.config.get('wgUserLanguage')] || this.stringShowAll['en']);
		
		//Remove old cookie (has wrong path, too long expiry)
		this.deleteOldCookieLanguage();

		//disabling the gadget on special pages
		if (mw.config.get('wgCanonicalNamespace') == "Special") return;

                // disabling on non files for non commons wikis
                if ((wgNoticeProject != 'commons') && (mw.config.get('wgNamespaceNumber') !=6)) return;

                // disabling on local files for non commons wikis
                if ((wgNoticeProject != 'commons') && (mw.config.get('wgArticleId')!=0)) return;

		// only activated in view , purge, historysubmit or submit mode
		if (!((mw.config.get('wgAction') == 'view') || (mw.config.get('wgAction') == 'purge') || (mw.config.get('wgAction') == 'edit') || (mw.config.get('wgAction') == 'historysubmit') || (mw.config.get('wgAction') == 'submit'))) return;

		var collapsDesc = false;

		// grab an array of multilingual elements
		this.mls = $('.multilingual');

		//Find {{en|...}} parent elements
		this.dls = $('div.description[lang]').parent();

		//Only collaps if more than 4 descriptions
		this.dls.each(function () {
			if ($(this).find('[lang]').length > 4 && $(this).attr('id') != 'bodyContent' && $(this).attr('id') != 'wikiPreview' && $(this).attr('id') != 'LangTableLangs') collapsDesc = true;
		});

		// Only build form if there are MLDs on page.
		if (!this.mls.length && !collapsDesc) return;

		if (collapsDesc) this.mls = this.dls.add('.multilingual');

		this.buildWidget();
		this.apply(this.getLanguage());
	},

	apply: function (language) {
		// if language is blank, delete the cookie and then recalculate
		if (!language) {
			this.deleteCookieLanguage();
			var language = this.getLanguage();
		}

		this.mls.each(function () {
			if ($(this).parent('[class^="image_annotation_content"]').length) return true;
			
			if ($(this).parents('#template-picture-of-the-day').length) return true;
	
			if ($(this).attr('id') == 'bodyContent' || $(this).attr('id') == 'wikiPreview' || $(this).attr('id') == 'LangTableLangs') return true;

			var $requestedLang = $(this).find('[lang=' + language + ']');

			if ($requestedLang.length) {
				$(this).children('[lang!=' + language + ']').hide();
				$(this).children('[lang=' + language + ']').show();
			} else {
				$(this).children('[lang]').show();
			}
		});
	}
};
// register as onload function
$(document).ready(function () {
	multilingual.init();
});